package com.zut.auth.application.controller;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.zut.auth.application.convert.AuthRoleDTOConverter;
import com.zut.auth.application.convert.AuthUserDTOConverter;
import com.zut.auth.application.dto.AuthRoleDTO;
import com.zut.auth.application.dto.AuthUserDTO;
import com.zut.common.entity.Result;
import com.zut.domain.entity.AuthRoleBO;
import com.zut.domain.entity.AuthUserBO;
import com.zut.domain.sevice.AuthRoleDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/role/")
public class RoleController {

    @Resource
    private AuthRoleDomainService authRoleDomainService;

    /**
     * 添加角色
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO) {
        Result result =new Result();
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.add.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            //参数校验
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()), "角色名不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()), "角色唯一标识不能为空");

            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            return result.success(authRoleDomainService.add(authRoleBO));
        } catch (Exception e) {
            log.error("RoleController.register.error:{}", e.getMessage(), e);
            return result.fail("注册用户失败");
        }
    }

    /**
     * 修改用户信息
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
        Result result =new Result();
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.update.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            //参数校验
            Preconditions.checkNotNull(authRoleDTO.getId(),"角色id不能为空");

            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            return result.success(authRoleDomainService.update(authRoleBO));
        } catch (Exception e) {
            log.error("RoleController.update.error:{}", e.getMessage(), e);
            return result.fail("更新用户信息失败");
        }
    }


    /**
     * 删除用户
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO) {
        Result result=new Result();
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.delete.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            return result.success(authRoleDomainService.delete(authRoleBO));
        } catch (Exception e) {
            log.error("RoleController.update.error:{}", e.getMessage(), e);
            return result.fail("删除用户信息失败");
        }
    }



}
